package cassandrawindow;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableDomainDateV2 {
    public static final String KEYSPACE                         =   "AdsData";
    public static final String ColumFamly                       =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf     =   "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf      =   "BannerViewCountCf";

    public static final String ColumFamlyCampaignClickCountCf   =   "CampaignCountCf";
    public static final String ColumFamlyCampaignViewCountCf    =   "CampaignViewCountCf";

    public static final String ColumFamlyDomainDateCf           =   "BzSuperBannerDomainDateCf";
    public static final String ColumFamlyZoneDateCf             =   "BzSuperBannerZoneDateCf";
    public static final String BzSuperBannerDomain6HDateCf      =   "BzSuperBannerDomain6HDateCf";

    public static long checkDateDomainDate = 0;
    
    public void handleData() throws ParseException, InterruptedException
    {
        long first_time = System.currentTimeMillis();
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);        
        MysqlDataManager mysql = new MysqlDataManager();        
        MysqlDataManagerMain mysqlMain = new MysqlDataManagerMain();        
        String currentDate = handleDate();
        //String currentDate = CONSTANT.Date;
        Debug.println("ngay chay la:"+currentDate);        
        //cassAct.getDomaindate(currentDate, ColumFamlyDomainDateCf, mysql, mysqlMain);
        cassAct.getDomaindateCPM(currentDate, BzSuperBannerDomain6HDateCf, mysql, mysqlMain);
        
        long last_time = System.currentTimeMillis();
        Debug.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableDomainDate");
    }
    /*
     * Lay thoi gian tu 6h sang ngay hom truoc toi 5h59 sang ngay hom sau
     * Coi la 1 ngay
     */
    public String handleDate()
    {
        String retDate = "";//2012-07-24
        String date = TimeUtil.getCurrentTimeInStringForLog();//yyyy/MM/dd H:mm:ss
        
        String[] arrTime        = new String[2];
        String[] firstArrTime   = new String[3];
        String[] secondArrTime  = new String[3];

        arrTime = date.split(" ");
        firstArrTime    = arrTime[1].split(":");
        secondArrTime   = arrTime[0].split("-");
        //init checkDate if trigger from 0h -> 6h
        if(checkDateDomainDate == 0)
        {
            checkDateDomainDate = TimeUtil.getCurrentTime();
        }
        
        if(Integer.valueOf(firstArrTime[0]) >= 6 && Integer.valueOf(firstArrTime[0]) <= 23)
        {
            checkDateDomainDate = TimeUtil.getCurrentTime();
            retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+secondArrTime[2];
        } else if (Integer.valueOf(firstArrTime[0]) >= 0 && Integer.valueOf(firstArrTime[0]) < 6 && TimeUtil.getCurrentTime() >= checkDateDomainDate)
        {
            Debug.println(date + "_" + TimeUtil.getCurrentTime() + "_" + checkDateDomainDate);
            int day = Integer.valueOf(secondArrTime[2]) - 1;
            retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+String.valueOf(day);
        }        
        return retDate;
    }
}
